﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Job.Logic.Company;
using Job.Entity;
using Job.Logic.Common;
using Job.Web.Framework;

namespace Job.Web.userControls.common
{
    public partial class PersonList :BaseCtrl
    {
        private int SearcherId
        {
            get { return myR["id"].IntValue; }
        }
        private string SearcherKey
        {
            get { return myR["key"].StringValue; }
        }

        SearcherLogic logic = new SearcherLogic();
        JobCompanySearcher searcher = new JobCompanySearcher();

        protected void Page_Load(object sender, EventArgs e)
        {
            MySession session = new MySession();
            if (session.GetCompanyFromSession() == null)
            {
                ShowError("您无权限查询求职者信息！");
                return;
            }
            if (!IsPostBack)
            {
                 try
                 {
                     if (SearcherId > 0)
                        searcher = logic.GetSearcher(SearcherId);
                     else if (SearcherKey != null && SearcherKey.Length > 0)
                     {
                         if (Cache[SearcherKey] != null)
                             searcher = (JobCompanySearcher)Cache[SearcherKey];
                         else
                             searcher = new JobCompanySearcher();

                         Cache.Remove(SearcherKey);
                     }
                     BindCount(searcher);
                     BindData(searcher);
                 }
                 catch (Exception ex)
                 {
                     ShowError("系统内部异常，暂时不能搜索求职者！");
                 }                
            }
        }
        
        private void BindCount(JobCompanySearcher searcher) 
        {
            try
            {
                MySession session = new MySession();
                string companyName = session.GetCompanyFromSession().CompanyName;
                int count = logic.CountPersonBySearcher(searcher, companyName);
                if (count > 0)
                {
                    pagerButtom.RecordCount = count;
                    pagerButtom.DataBind();
                    pagerButtom.Visible = true;
                }
                else
                {
                    ShowError("未查询到符合条件的求职者！");
                }

            }
            catch (Exception ex)
            {
                ShowError("查询搜索器失败！");
            }
        }

        private void BindData(JobCompanySearcher searcher)
        {
            try
            {
                int pageSize = pagerButtom.PageSize;
                int pageIdx = pagerButtom.CurrentPageIndex;
                
                MySession session = new MySession();
                string companyName = session.GetCompanyFromSession().CompanyName;

                DataTable dt = logic.GetPagePersonBySearcher(searcher,companyName,pageSize, pageIdx);
                if (dt == null)
                {
                    ShowError("未查询到符合条件的求职者！");
                }
                else
                {
                    this.PersonGrid.DataSource = dt;
                    this.PersonGrid.DataBind();
                }
            }
            catch (Exception ex)
            {
                ShowError("查询求职者列表失败！");
            }

        }


        protected void PersonGrid_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.RowIndex > -1)
                {
                    try
                    {
                        Label lbPersonId = (Label)e.Row.FindControl("lbPersonId");
                        HyperLink hlPerson = (HyperLink)e.Row.FindControl("hlPerson");
                        hlPerson.NavigateUrl = WebCommon.GetApplicationPath(Request)
                            + "Common/ResumeBrowse.aspx?categoryid=1&resumeid=" + lbPersonId.Text;

                        Label lbSexy = (Label)e.Row.FindControl("lbSexy");
                        if ( lbSexy.Text.Equals("1"))
                            lbSexy.Text = "男";
                        else if ( lbSexy.Text.Equals("2"))
                            lbSexy.Text = "女";
                        else
                            lbSexy.Text = "";

                        Label lbEdu = (Label)e.Row.FindControl("lbEdu");
                        lbEdu.Text = MemoryDictionary.GetCommonDictName(lbEdu.Text);
      
                        Label lbAddr = (Label)e.Row.FindControl("lbAddr");
                        lbAddr.Text = MemoryDictionary.GetFullAddrName(lbAddr.Text);

                        Label lbJobStaus = (Label)e.Row.FindControl("lbJobStatus");
                        lbJobStaus.Text = MemoryDictionary.GetCommonDictName(lbJobStaus.Text);

                    }
                    catch (Exception ex)
                    { }
                }
            }
        }




        protected void pagerButtom_PageChanged(object sender, EventArgs e)
        {
            BindData(this.searcher);
        }

        private void ShowError(string message)
        {
            this.PersonGrid.Visible = false;
            this.msgTab.Visible = true;
            this.msgTd.InnerHtml = "<font color='#990000'>" + message + "</font>";
            this.pagerButtom.Visible = false;
        }
    }
}